/**
 * Copyright (c) 2011 Sylvain Gougouzian (sylvain@gougouzian.fr)
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 * GNU GPL (http://www.gnu.org/licenses/gpl.html) licensed.
 *
 * jQuery moodular controls by Sylvain Gougouzian http://sylvain.gougouzian.fr
 */
jQuery(function($){
	$.extend($.fn.moodulite.controls.init, {
		keys: function(m){
			$(document).keydown(function(event){
				if ((event.keyCode == 39) || (event.keyCode == 40)) {
					m.next();
					return false;
				}
				if ((event.keyCode == 37) || (event.keyCode == 38)) {
					m.prev();
					return false;
				}
			});
		},
		index: function(m){
			m.e.parent().parent().append('<ul class="moodulite_itemList"></ul>');
			var h = "";
			for (var i = 0; i < m.nbItems; i++) {
				h += '<li class="moodulite_itemList_li" rel="' + i + '">' + (i + 1) + '</li>';
			}
			$('.moodulite_itemList', m.e.parent().parent()).html(h);
			$('.moodulite_itemList_li', m.e.parent().parent()).css('cursor', 'pointer').click(function(){
				$('.moodulite_itemList_li.active', m.e.parent().parent()).removeClass('active');
			$('.moodulite_itemList_li').eq(m.current + 1).addClass('active');
				$(this).addClass('active');
				if(!m.locked) {
					m.moveTo($(this).attr('rel'));
				} else { 
					m.locked = false;
				}
				return false;
			});
			$('.moodulite_itemList_li:first').addClass('active');
		},
		wheel: function(m){
			m.e.parent().parent().bind("mousewheel", function(event, delta){
				var dir = delta > 0 ? 'Up' : 'Down';
				if (dir == 'Up') {
					m.next();
				}
				else {
					m.prev();
				}
				return false;
			});
		},
		touch: function(m){
			m.touchBPosX = null;
			m.touchBPosY = null;
			m.touchEPosX = null;
			m.touchEPosY = null;
			m.e.parent().bind('touchstart', function (event) {
				var e = event.originalEvent;
				m.touchBPosX = e.targetTouches[0].pageX;
				m.touchBPosY = e.targetTouches[0].pageY;
			}).bind('touchmove', function (event) {
				event.preventDefault();
				var e = event.originalEvent;
				m.touchEPosX = e.targetTouches[0].pageX;
				m.touchEPosY = e.targetTouches[0].pageY;
			}).bind('touchend', function(e) {
				if (m.vertical) {
					if (m.dir == 1) {
						if (m.touchEPosY < m.touchBPosY)
							m.next();
						else
							m.prev();
					}
					else {
						if (m.touchEPosY > m.touchBPosY)
							m.next();
						else
							m.prev();
					}
				}
				else {
					if (m.dir == 1) {
						if (m.touchEPosX < m.touchBPosX)
							m.next();
						else
							m.prev();
					}
					else {
						if (m.touchEPosX > m.touchBPosX)
							m.next();
						else
							m.prev();
					}
				}
				m.touchBPosX = null;
				m.touchBPosY = null;
				m.touchEPosX = null;
				m.touchEPosY = null;
				return false;
			});
		}
	});

	$.extend($.fn.moodulite.controls.before, {
		index: function (m) {
			$('.moodulite_itemList_li.active', m.e.parent().parent()).removeClass('active');
			var n = m.current + 1;
			if (n == m.nbItems) n = 0;
			$('.moodulite_itemList_li', m.e.parent().parent()).eq(n).addClass('active');
		},
	});

	$.extend($.fn.moodulite.controls.after, {
	});
	
});